﻿using System;
using System.Reflection;

namespace TA.DotNetUtils
{
	public class ReflectionPropertiesAccessor<T> : IPropertiesAccessor<T>
	{

		#region IPropertiesAccessor<T> Members

		public object GetValue(T t, string fieldName)
		{
			PropertyInfo pinfo = t.GetType().GetProperty(fieldName);
			if (pinfo == null)
				throw new ArgumentException("Property '" + fieldName + "' not found.");
			return pinfo.GetValue(t, new object[0]);
		}

		public void SetValue(T t, string fieldName, object value)
		{
			PropertyInfo pinfo = t.GetType().GetProperty(fieldName);
			if (pinfo == null)
				throw new ArgumentException("Property '" + fieldName + "' not found.");
			pinfo.SetValue(t, value, new object[0]);
		}

		#endregion
	}
}
